package com.drone.drone_demo.service;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class JsonFileService {
    public List<Map<String, Object>> readFlightDataAsList(String filePath) {
        try {
            String jsonContent = new String(Files.readAllBytes(Paths.get(filePath)));

            JsonArray jsonArray = JsonParser.parseString(jsonContent).getAsJsonArray();

            // 用 Gson 转换为 List<Map>
            Gson gson = new Gson();
            Type type = new TypeToken<List<Map<String, Object>>>() {}.getType();
            return gson.fromJson(jsonArray, type);

        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }
}
